package com.yds.developtools.utils

import android.os.SystemClock
import android.view.View
import android.view.View.OnClickListener

/**
 * 全局延时点击事件
 * mBinding.mIvBack.setOnClickListener(YDSOnDelayClickListener {
 *             onBackPressed()
 *         })
 */
class YDSOnDelayClickListener(delay: Long = 1000, callback: () -> Unit) : OnClickListener {
    private var delayTime = delay
    private var clickCallBack: () -> Unit = callback

    private var startTime = 0L

    override fun onClick(view: View?) {
        val elapsedRealtime = SystemClock.elapsedRealtime()
        if (elapsedRealtime - startTime >= delayTime) {
            clickCallBack()
            startTime = elapsedRealtime
        }
    }
}